
const fs = require('fs');
const radius = 640;  //地球半径
var centerPoints = {};
var henanCenter = [113.665412,34.757975];
var innerBound = [];
function changeDataToThree(){
    fs.readFile("./orangeDataBound/4101.json",{encoding:"utf8"},function(err,data){
        if (err) throw err;
        let gomData = JSON.parse(data).features,f_num = gomData.length;
        centerPoints = getCenter(henanCenter[0],henanCenter[1]);
        for(let j=0;j<f_num;j++){
            let boundData = gomData[j].geometry.coordinates[0][0],num = boundData.length;
            let cityCenter = getCityCenter(gomData[j].properties.center[0],gomData[j].properties.center[1]);
            let cityName = gomData[j].properties.name;
            let points = [];
            for(let i=0;i<num;i++){
                let point = lglt2xyz(boundData[i][0],boundData[i][1]);
                // points.push(point.x,point.y,point.z);
                points.push(point.x,point.y,0); //高度信息设置为0
            }
            innerBound.push({cityCenter,cityName,bounder:points});
        }
        fs.writeFile("./henanBound/4101.text",JSON.stringify(innerBound),{encoding:"utf8"},function(err){
            if (err) throw err;
            console.log('The file has been saved!');
        })
    })
}

function lglt2xyz(longitude,latitude){
    const phi = (180 + longitude) * (Math.PI / 180);
    const theta = (90 - latitude) * (Math.PI / 180);
    let px = -radius * Math.sin(theta) * Math.cos(phi);
    let py = radius * Math.cos(theta);
    let pz = radius * Math.sin(theta) * Math.sin(phi);
    return {
      x: -Number(((px-centerPoints.x)*10).toFixed(3)),
      y: Number(((py-centerPoints.y)*10).toFixed(3)),
      z: Number((pz-centerPoints.z).toFixed(3)),
    }
}
function getCityCenter(longitude,latitude){
    const phi = (180 + longitude) * (Math.PI / 180);
    const theta = (90 - latitude) * (Math.PI / 180);
    
    let px = -radius * Math.sin(theta) * Math.cos(phi);
    let py = radius * Math.cos(theta);
    let pz =  radius * Math.sin(theta) * Math.sin(phi);

    return {
        x: -Number(((px-centerPoints.x)*10).toFixed(3)),
        y: Number(((py-centerPoints.y)*10).toFixed(3)),
        z: Number((pz-centerPoints.z).toFixed(3)),
    }
}
function getCenter(longitude,latitude){
    const phi = (180 + longitude) * (Math.PI / 180);
    const theta = (90 - latitude) * (Math.PI / 180);
    return {
      x: -radius * Math.sin(theta) * Math.cos(phi),
      y: radius * Math.cos(theta),
      z: radius * Math.sin(theta) * Math.sin(phi),
    }
}

function degToRad(deg){
    return deg*Math.PI/180;
}

changeDataToThree();